package com.chuzhong.sf.gui;

import com.chuzhong.sf.api.admin_api;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 初衷.
 * @Date 2020/10/15
 **/
public class new_password extends JFrame{

    private static int count=0;
    private static JButton bt1;//确定按钮
    private static JButton bt2;//取消按钮
    private static JLabel jl_1;//登录的版面
    private static JFrame jf_1;//登陆的框架
    private static JTextField jtext1;//用户名
    private static JLabel jl_admin;

public new_password(String username,Integer id){
    Font font =new Font("宋体", Font.PLAIN, 20);//设置字体
    Font font1 =new Font("宋体", Font.PLAIN, 25);//设置字体
    jf_1=new JFrame("重置密码");
        jf_1.setSize(500, 300);

    jl_1=new JLabel();

    jl_admin=new JLabel("新密码:");
        jl_admin.setBounds(50, 50, 100, 50);
        jl_admin.setFont(font1);

    bt1=new JButton("确定");         //更改成loginButton
        bt1.setBounds(120, 150, 100, 50);
        bt1.setFont(font);

    bt2=new JButton("取消");         //更改成loginButton
    bt2.setBounds(280, 150, 100, 50);
    bt2.setFont(font);


    //加入文本框
        jtext1=new JTextField();
        jtext1.setBounds(150, 50, 250, 50);
        jtext1.setFont(font);

        jl_1.add(jtext1);
        jl_1.add(jl_admin);
        jl_1.add(bt1);
        jl_1.add(bt2);

        jf_1.add(jl_1);
        jf_1.setVisible(true);
        jf_1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        jf_1.setLocation(300,400);
        jf_1.setLocationRelativeTo(null);

    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                if(jtext1.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"密码输入为空，请重新输入" );
                }else {
                    Integer updatepassword = admin_api.admin_updatepassword(username, jtext1.getText(), id);
                    if (updatepassword.equals(1)){
                        JOptionPane.showMessageDialog(null,"重置密码成功！" );
                        jf_1.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null,"重置密码失败！请联系管理员");
                    }

                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    };
    bt1.addActionListener(actionListener);

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jf_1.dispose();
        }
    };
    bt2.addActionListener(listener);
}
}
